package com.leonzx.base.service.system;

import com.leonzx.base.dao.system.MenuRoleDAO;
import com.leonzx.base.entity.system.MenuRole;
import com.leonzx.base.service.BaseService;
import com.leonzx.base.utils.Constants;
import com.leonzx.base.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-20 15:43
 */
@Service
public class MenuRoleService extends BaseService {
    @Autowired
    private MenuRoleDAO menuRoleDAO;

    public void saveOrUpdate(MenuRole menuRole) {
        menuRole.setCreateTime(new Date());
        Example example = new Example(MenuRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", menuRole.getRoleId());
        criteria.andEqualTo("menuId", menuRole.getMenuId());
        Integer is = menuRoleDAO.selectCountByExample(example);
        if (is > 0) {
            menuRoleDAO.updateByExample(menuRole, example);
        } else {
            menuRole.setId(UUIDUtils.getUUID());
            menuRoleDAO.insert(menuRole);
        }

    }

    /**
     * 根据角色，清空所有权限，state = 0
     *
     * @param roleId
     */
    public void delAllByRoleId(String roleId) {
        Example example = new Example(MenuRole.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        MenuRole menuRole = new MenuRole();
        menuRole.setState(Constants.STATE_INVAILD);
        menuRoleDAO.updateByExample(menuRole, example);
    }

}
